package basic.listener;

import org.springframework.boot.context.event.*;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class SpringLifeListener implements ApplicationListener, EnvironmentAware {
    public SpringLifeListener() {
        System.out.println("bean构造方法");
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if(event instanceof ApplicationStartingEvent){
            System.out.println("应用程序启动中");
        } else if (event instanceof ApplicationEnvironmentPreparedEvent) {
            System.out.println("初始化环境变量");
        } else if (event instanceof ApplicationPreparedEvent) {
            System.out.println("初始化环境变量完成，初始化上下文context完成");
        } else if (event instanceof ContextRefreshedEvent) {
            System.out.println("应用刷新");
        } else if (event instanceof ApplicationReadyEvent) {
            System.out.println("应用已启动完成");
        } else if (event instanceof ContextStartedEvent) {
            System.out.println("应用启动好了");
        } else if(event instanceof ApplicationFailedEvent){
            System.out.println("应用启动失败");
        }else if (event instanceof ContextStoppedEvent) {
            System.out.println("应用停止");
        } else if (event instanceof ContextClosedEvent) {
            System.out.println("应用关闭");
        } else {
            System.out.println("其他事件" + event.toString());
        }
    }

    @Override
    public void setEnvironment(Environment environment) {
        System.out.println("执行setEnvironment");
    }
}
